import cgi
import os, sys, random
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from cointoss import CoinToss
from guestbook import Guestbook
from geohashing import GeoHashing
from flag_quiz.app import FlagQuiz
from me import me_xpm

class MainPage(webapp.RequestHandler):
    def get(self):
        html = '''
<html>
<head>
  <script src="/js/base.js"></script>
  <script src="/js/wacky_base64.js"></script>
  <script src="/js/sha2.js"></script>
  <script src="/js/aes.js"></script>
  <script src="/js/widgets.js"></script>
</head>
<body>
<div align="center">
  <a href="flag">flag quiz</a> | 
  <a href="geohashing">geo hashing</a> | 
  <a href="yourstruly">yours truly</a>
</div>
<div id="p1" postit='<div align="center">Countdown</div>' width=480 height=240 left="c-240" top="150">
  <div id="t1" type=typist interval=30000 speed=300 style="font-size:24pt" align="center">Countdown</div>
  <br>
  <div id="ctd1" type=countdown till="2010/6/11 17:00:00" align="center" heading="Big Day" message="Meh" style="font-size:20pt;color:#6699cc"></div>
</div>
</body>
<script>
POSTIT.render(document.getElementsByTagName("div"));
TYPIST.setup(document.getElementById("t1"));
COUNTDOWN.render("ctd1");
</script>
</html>
'''
        self.response.out.write(html)

class YoursTruly(webapp.RequestHandler):
    def get(self):
        meta = me_xpm[0].split(' ')
        width = int(meta[0])
        height = int(meta[1])
        colors = int(meta[2])
        bytes = int(meta[3])
        color_map = {}
        html = '<table align="center" cellspacing="4" cellpadding="0">\n'
        for x in me_xpm[1:colors+1]:
            color_map[x[:bytes]] = x[5:]
        mosaic = 4
        for x in me_xpm[colors+1:]:
            html += '<tr height="%d">' % mosaic
            for i in range(0, len(x), bytes):
                html += '<td style="background:%s" width="%d"/>' % (color_map[x[i:i+bytes]], mosaic)
            html += '</tr>\n'
        html += '</table>\n'
        self.response.out.write(html)

class Env(webapp.RequestHandler):
    def get(self):
        text = ' '.join(dir(self))+'<br>Params: '+repr(self.request.params)+'<br>'
	text += '<br>Path:'+' '.join(sys.path)+'<br>'
        text += '<br>'.join(dir(self.request))
        self.response.out.write(text)

application = webapp.WSGIApplication(
    [('/', MainPage),
     ('/ct', CoinToss),
     ('/gb', Guestbook),
     ('/flag', FlagQuiz),
     ('/yourstruly', YoursTruly),
     ('/geohashing', GeoHashing),
     ('/env', Env)],
    debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
